package com.framework.internationalization.config;

import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import javax.validation.Validator;
import java.util.Locale;
import java.util.Properties;

/**
 * @author 龘鵺
 * @className com.framework.internationalization.config
 * @description 提示信息国际化消失初始化配置
 * @datetime 2025-01-09 11:11:11
 * @version 1.0
 */
@AutoConfiguration
public class ValidatorConfig {
    @Autowired
    private MessageSource messageSource;

//    /**
//     * @return org.springframework.web.servlet.i18n.LocaleChangeInterceptor
//     * @title 设置国际化拦截器语言参数自定义键
//     * @description 设置国际化拦截器语言参数自定义键
//     * @author 龘鵺
//     * @datetime 2025-01-09 11:11:11
//     */
//    @Bean
//    public LocaleChangeInterceptor localeChangeInterceptor() {
//        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
//        //自定义获取语言参数键设置
//        localeChangeInterceptor.setParamName("language");
//        return localeChangeInterceptor;
//    }
//
//
//    /**
//     * @return org.springframework.web.servlet.LocaleResolver
//     * @title 设置默认返回语言
//     * @description 设置默认返回语言
//     * @author 龘鵺
//     * @datetime 2025-01-09 11:11:11
//     */
//    @Bean
//    public LocaleResolver localeResolver() {
//        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
////        //指定默认语言为中文
////        sessionLocaleResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
//        //指定默认语言为英语
//        sessionLocaleResolver.setDefaultLocale(Locale.US);
//        return sessionLocaleResolver;
//    }
//
//    /**
//     * @return org.springframework.context.support.ResourceBundleMessageSource
//     * @title 初始化加载自定义国际化文件配置
//     * @description 初始化加载自定义国际化文件配置
//     * @author 龘鵺
//     * @datetime 2025-01-09 11:11:11
//     */
//    @Bean(name = "messageSource")
//    public ResourceBundleMessageSource resourceBundleMessageSource() {
//        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
//        //指定国际化的Resource Bundle地址
//        resourceBundleMessageSource.setBasename("i18n/messages");
//        //指定国际化的默认编码
//        resourceBundleMessageSource.setDefaultEncoding("UTF-8");
//        return resourceBundleMessageSource;
//    }

    /**
     * @return javax.validation.Validator
     * @title 国际化校验初始化配置
     * @description 国际化校验初始化配置
     * @author 龘鵺
     * @datetime 2025-01-09 11:11:11
     */
    @Bean
    public Validator validator() {
        //ValidationMessages_zh_CN.properties
        //ValidationMessages.properties
//        HibernateValidator
        // 本地验证器工厂Bean
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        // 设置使用 HibernateValidator 校验器
        localValidatorFactoryBean.setProviderClass(HibernateValidator.class);
        // 国际化
        localValidatorFactoryBean.setValidationMessageSource(messageSource);
        // 设置 快速异常返回
        localValidatorFactoryBean.getValidationPropertyMap().put("hibernate.validator.fail_fast", "true");
        // 加载配置
        localValidatorFactoryBean.afterPropertiesSet();
        return localValidatorFactoryBean;
    }
}
